﻿@addTagHelper Smartstore.Web.TagHelpers.Admin.BootstrapIconTagHelper, Smartstore.Web.Common

@using Smartstore.Utilities

@functions
{
    private string Value => ViewData.Model != null ? Convert.ToString(ViewData.Model) : null;
}

@{
    Assets.AppendScriptFiles("~/js/smartstore.linkbuilder.js");

    var rnd = CommonHelper.GenerateRandomInteger();
    var id = Html.IdForModel();
    var metadata = LinkResolver.GetBuilderMetadata().ToDictionarySafe(x => x.Schema, StringComparer.OrdinalIgnoreCase);
    var link = await LinkResolver.ResolveAsync(Value);
    var expression = link.Expression;
    var linkTarget = expression.LinkTarget;
    var hideQueryString = GetMetadata<bool>("hideQueryString");
    var hideLinkTarget = GetMetadata<bool>("hideLinkTarget");
    var arr = GetMetadata<string>("allowedSchemas").SplitSafe(',').Select(x => x.ToLowerInvariant());
    var allowedSchemas = arr.Any() ? arr : metadata.Keys;
    var size = GetMetadata<string>("size");
    var gutterSize = size == "sm" ? 1 : 2;
}

<div id="link-builder-@rnd" class="link-builder edit-control"
     data-field-id="@id"
     data-editor="link"
     data-current-type="@(Value != null ? expression.Schema : string.Empty)">

    @* Template control that receives the link expression. *@
    <input asp-for="@Model" type="hidden" value="@Value" />

    <div class="row flex-nowrap g-@gutterSize">

        @* 1. prepend type selector. *@
        <div class="link-type-container col-auto">
            <button type="button" class="btn btn-secondary btn-linktype btn-icon dropdown-toggle" attr-class='(size.HasValue(), "btn-" + size)' data-toggle="dropdown" title="">
                <i class="fa fa-link"></i>
            </button>
            <div class="dropdown-menu">
                @foreach (var schema in allowedSchemas)
                {
                    var info = metadata[schema];
                    <a class="dropdown-item link-type" href="#" data-type="@schema">
                        <i class="fa-fw @info.Icon"></i>
                        <span>@T(info.ResKey)</span>
                    </a>
                }
            </div>
        </div>

        @* 2. main control. *@
        @foreach (var schema in allowedSchemas)
        {
            var info = metadata[schema];
            var label = expression.Schema == schema ? link.Label : string.Empty;
            var value = expression.Schema == schema && expression.Target != null
                ? (link.EntityId.HasValue ? link.EntityId.Value.ToStringInvariant() : expression.Target.EmptyNull())
                : string.Empty;

            <div class="link-control col" attr-class='(expression.Schema != schema, "hide")' data-type="@schema">
                @if (info.Widget != null)
                {
                    @await info.Widget.InvokeAsync(new WidgetContext(ViewContext, (link, label, value)))
                }
                else if (schema == DefaultLinkProvider.SchemaProduct)
                {
                    <div class="input-group" attr-class='(size.HasValue(), "input-group-" + size)'>
                        <input type="text" class="form-control resettable product-picker-input bg-white text-muted" value="@label" readonly />
                        <div class="input-group-append">
                            <entity-picker max-items="1"
                                           onselectioncompleted="lbPickerSelectionCompleted@(rnd.ToString())"
                                           append-mode="false"
                                           icon-css-class=""
                                           dialog-title="@T("Common.Entity.SelectProduct").Value"
                                           caption="&hellip;" />
                        </div>
                    </div>
                }
                else if (schema == DefaultLinkProvider.SchemaCategory)
                {
                    <select class="form-control transferable resettable"
                            attr-class='(size.HasValue(), "form-control-" + size)'
                            data-select-url="@Url.Action("AllCategories", "Category", new { area = "Admin" })"
                            data-select-selected-id="@value"
                            data-select-init-text="@label"></select>
                }
                else if (schema == DefaultLinkProvider.SchemaManufacturer)
                {
                    <select class="form-control transferable resettable"
                            attr-class='(size.HasValue(), "form-control-" + size)'
                            data-select-url="@Url.Action("AllManufacturers", "Manufacturer", new { area = "Admin" })"
                            data-select-selected-id="@value"
                            data-select-init-text="@label"></select>
                }
                else if (schema == DefaultLinkProvider.SchemaTopic)
                {
                    <select class="form-control transferable resettable"
                            attr-class='(size.HasValue(), "form-control-" + size)'
                            data-select-url="@Url.Action("AllTopics", "Topic", new { area = "Admin" })"
                            data-select-selected-id="@value"
                            data-select-init-text="@label"></select>
                }
                else if (schema == "url")
                {
                    <input type="text" class="form-control transferable resettable" attr-class='(size.HasValue(), "form-control-" + size)' value="@value" />
                }
                else if (schema == "file")
                {
                    <div class="input-group" attr-class='(size.HasValue(), "input-group-" + size)'>
                        <input id="lb-file-url-@rnd" type="text" class="form-control transferable resettable bg-white text-muted" value="@label" readonly />
                        <div class="input-group-append">
                            <a href="javascript:;"
                               class="btn btn-secondary browse-files"
                               data-file-manager-url="@Display.GetFileManagerUrl()"
                               data-field-id="lb-file-url-@rnd"
                               title="@(T("Common.BrowseFiles"))&hellip;">
                                <i class="fa fa-upload"></i>
                                <span sm-if='size != "sm"'>@(T("Common.BrowseFiles"))&hellip;</span>
                            </a>
                        </div>
                    </div>
                }
            </div>
        }

        @* 3. append additional buttons. *@
        <div class="col-auto">
            <div class="btn-group">
                <button type="button" class="btn btn-secondary btn-icon btn-to-danger btn-reset" attr-class='(size.HasValue(), "btn-" + size)' title="@T("Common.Remove")">
                    <i class="fa fa-unlink"></i>
                </button>
                <div sm-if="!hideLinkTarget" class="btn-group">
                    <button type="button"
                            class="btn btn-secondary btn-icon btn-link-target dropdown-toggle"
                            attr-class='(size.HasValue(), "btn-" + size)'
                            title="@T("LinkBuilder.LinkTarget")"
                            data-toggle="dropdown"
                            aria-haspopup="true"
                            aria-expanded="false">
                        <i class="far fa-window-restore @(linkTarget.HasValue() ? "text-success" : "text-muted")"></i>
                    </button>
                    <div class="dropdown-menu dropdown-menu-check dropdown-menu-right dropdown-link-target">
                        <a class="dropdown-item" href="#" data-value="" attr-class='(linkTarget.IsNullOrEmpty(), "checked")'>@T("Common.Unspecified")</a>
                        <a class="dropdown-item" href="#" data-value="_self" attr-class='(linkTarget == "_self", "checked")'>_self</a>
                        <a class="dropdown-item" href="#" data-value="_blank" attr-class='(linkTarget == "_blank", "checked")'>_blank</a>
                        <a class="dropdown-item" href="#" data-value="_parent" attr-class='(linkTarget == "_parent", "checked")'>_parent</a>
                        <a class="dropdown-item" href="#" data-value="_top" attr-class='(linkTarget == "_top", "checked")'>_top</a>
                    </div>
                </div>

                <div sm-if="!hideQueryString" class="btn-group dropdown-container dropdown-focus">
                    <button type="button"
                            class="btn btn-secondary btn-icon btn-query-string dropdown-toggle@(size.HasValue() ? " btn-" + size : "")"
                            attr-class='(expression.Schema == "url", "hide")'
                            title="@T("Common.QueryString")"
                            data-toggle="dropdown"
                            aria-haspopup="true"
                            aria-expanded="false"
                            data-boundary="window">
                        <i class="far fa-circle-question @(expression.Query.HasValue() ? "text-success" : "text-muted")"></i>
                    </button>
                    <div class="dropdown-menu dropdown-menu-right">
                        <div class="px-2">
                            <input type="text"
                                   class="form-control transferable resettable query-string"
                                   attr-class='(size.HasValue(), "form-control-" + size)'
                                   value="@expression.Query.TrimStart('?')" placeholder="@T("Common.QueryString")"
                                   style="width: 350px;" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@if (allowedSchemas.Contains(DefaultLinkProvider.SchemaProduct))
{
    <script>
        function lbPickerSelectionCompleted@(rnd)(ids, selectedItems) {
            var cnt = $("#link-builder-@rnd");
            var val = 'product:' + ids[0];
            var qs = cnt.find(".query-string").val() || '';
            while (qs.startsWith('?')) {
                qs = qs.substring(1);
            }
            if (!_.isEmpty(qs)) {
                val = val + '?' + qs;
            }
            $('#@id').val(val).trigger("change");
            cnt.find(".product-picker-input").val(selectedItems[0].name);
            return true;
        }
    </script>
} 